﻿using IBll;
using IDal;
using MD5Hash;
using Microsoft.EntityFrameworkCore;
using Model;
using System.Runtime.InteropServices;

namespace Bll
{
    public class UserBll:IUserBll
    {
        private IUserDal<UserInfo> dal;
        private IUserDal<UserType> typedal;

        public UserBll(IUserDal<UserInfo> dal,IUserDal<UserType> typedal)
        {
            this.dal = dal;
            this.typedal = typedal;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Task<int> Add(UserInfo userInfo)
        {
            userInfo.UserPwd = userInfo.UserPwd.GetMD5();
           return dal.AddAsync(userInfo);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Task<int> Update(UserInfo userInfo)
        {
            return dal.UpdateAsync(userInfo);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public Task<int> Delete(int UserId)
        {
            var m = dal.GetAllAsync().FirstOrDefault(x=>x.UserId==UserId);
            return dal.DeleteAsync(m);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public dynamic GetAll(string? name,int? Kage,int? Jage,int? tid,int? sex)
        {
            var user = dal.GetAllAsync();
            var usertype = typedal.GetAllAsync();

            var list = from UserInfo in user
                       join UserType in usertype
                       on UserInfo.UserTypeId equals UserType.UserTypeId
                       select new
                       {
                           UserInfo.UserId,
                           UserInfo.UserName,
                           UserInfo.UserPhone,
                           UserInfo.UserPwd,
                           UserInfo.UserAge,
                           UserInfo.UserSex,
                           UserInfo.Start,
                           UserType.UserTypeId,
                           UserType.UserTypeName,
                       };
            //姓名
            if(!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.UserName.Contains(name));
            }
            //最大年龄
            if(Kage>0)
            {
                list = list.Where(x=>x.UserAge<=Kage);
            }
            //最小年龄
            if (Jage > 0)
            {
                list = list.Where(x=>x.UserAge>=Jage);
            }
            //下拉框查询
            if(tid>0)
            {
                list = list.Where(x=>x.UserTypeId==tid);
            }
            if(sex>0)
            {
                list = list.Where(x => x.UserSex == sex);
            }
            return list.ToList();
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public Task<int> IsDelete(int UserId)
        {
            var m = dal.GetAllAsync().FirstOrDefault(x => x.UserId == UserId);
            m.IsDelete = true;
            m.DeleteDate = DateTime.Now;
            return dal.UpdateAsync(m);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int Login(string name, string pwd)
        {
           var sql = dal.GetAllAsync().FirstOrDefault(x=>x.UserName==name);
            //没有这个用户返回1
            if(sql==null)
            {
                return 1;
            }
            //密码不等于这个加密密码返回3
            if(sql.UserPwd!=pwd.GetMD5())
            {
                return 3;
            }
            //正确返回2
            return 2;
        }


    }
}
